home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / spiderweb / tools / tie / tie.c < prev    next >
C/C++ Source or Header  |  1992-07-05  |  12KB  |  697 lines

  1. /*2:*/
  2. #line 66 "tie.web"
  3.  
  4. #include<stdio.h>
  5. /*56:*/
  6. #line 72 "character.web"
  7.  
  8. typedef char ASCII;
  9. typedef char outer_char;
  10.  
  11. /*:56*//*61:*/
  12. #line 133 "character.web"
  13.  
  14. typedef char ascii_code;
  15. typedef FILE *text_file;
  16.  
  17.  
  18. /*:61*/
  19. #line 68 "tie.web"
  20.  
  21. /*9:*/
  22. #line 28 "datadecl.web"
  23.  
  24. typedef char in_file_modes;
  25. typedef char file_types;
  26.  
  27. /*:9*//*10:*/
  28. #line 35 "datadecl.web"
  29.  
  30. typedef char boolean;
  31.  
  32. /*:10*//*11:*/
  33. #line 45 "datadecl.web"
  34.  
  35. typedef char out_md_type;
  36. typedef char *name_type;
  37. typedef int buffer_index;
  38. typedef int file_index;
  39. typedef struct{
  40. name_type name_of_file;
  41. ascii_code buffer[100+1];
  42. in_file_modes mode;
  43. int lineno;
  44. file_types type_of_file;
  45. int limit;
  46. }input_description;
  47.  
  48.  
  49.  
  50. /*:11*/
  51. #line 69 "tie.web"
  52.  
  53. /*8:*/
  54. #line 165 "tie.web"
  55.  
  56. int next_available_file= 0;
  57. #line 1 "datadecl.web"
  58. /*:8*//*12:*/
  59. #line 63 "datadecl.web"
  60.  
  61. int actual_input,test_input;
  62. boolean input_has_ended;
  63. input_description input_organization[9+1];
  64. text_file input_files[9+1];
  65. int no_ch;
  66. boolean prod_chf;
  67. out_md_type out_mode;
  68. int verbose= 0;
  69.  
  70.  
  71.  
  72. #line 168 "tie.web"
  73.  
  74.  
  75.  
  76.  
  77. /*:12*//*14:*/
  78. #line 190 "tie.web"
  79. boolean lines_dont_match();
  80. /*:14*//*16:*/
  81. #line 204 "tie.web"
  82. boolean e_of_ch_module();
  83.  
  84. /*:16*//*18:*/
  85. #line 219 "tie.web"
  86. boolean e_of_ch_module();
  87.  
  88. /*:18*//*21:*/
  89. #line 236 "tie.web"
  90. boolean file_open();
  91.  
  92. /*:21*//*40:*/
  93. #line 529 "tie.web"
  94. long int lines_processed= 0;
  95. /*:40*//*49:*/
  96. #line 11 "history.web"
  97.  
  98. int history;
  99.  
  100.  
  101. /*:49*//*57:*/
  102. #line 80 "character.web"
  103.  
  104. ASCII xord[0177];
  105. outer_char xchr[0200];
  106.  
  107. /*:57*/
  108. #line 70 "tie.web"
  109.  
  110. /*52:*/
  111. #line 25 "error.web"
  112.  
  113. print_name_of_file(cur_index)
  114. file_index cur_index;
  115. {
  116. fprintf(stderr,"%s",input_organization[cur_index].name_of_file);
  117. }
  118.  
  119.  
  120. /*:52*//*53:*/
  121. #line 36 "error.web"
  122.  
  123. err_loc(i)
  124. int i;
  125. {
  126. fprintf(stderr,
  127. " (file %s, l.%d).\n",input_organization[i].name_of_file,
  128. input_organization[i].lineno);
  129.  
  130. }
  131.  
  132. /*:53*//*55:*/
  133. #line 55 "error.web"
  134.  
  135. jump_out(){exit(history==0?0:1);}
  136. #line 1 "character.web"
  137. /*:55*/
  138. #line 71 "tie.web"
  139.  
  140.  
  141. main(argc,argv)
  142. int argc;
  143. char * *argv;
  144. {
  145. int i,j;
  146.  
  147. /*50:*/
  148. #line 17 "history.web"
  149.  
  150. history= 0;
  151. /*:50*/
  152. #line 79 "tie.web"
  153.  
  154. if(verbose)
  155. {fprintf(stderr,"This is TIE, C Version 0.1 (ported to C)");putc('\n',stderr);};
  156. /*7:*/
  157. #line 137 "tie.web"
  158.  
  159. while(--argc>0){
  160. if( * *++argv=='-')
  161. switch( *++ *argv){
  162. case'm':
  163. prod_chf= 0;
  164. break;
  165. case'c':
  166. prod_chf= 1;
  167. break;
  168. case'v':
  169. verbose++;
  170. break;
  171. default:
  172. fprintf(stderr,"Bad option -%c ignored\n",
  173.  * *argv);
  174. break;
  175. }
  176. else{
  177. if(next_available_file>9)
  178. {{putc('\n',stderr);fprintf(stderr,"too many files");};{fprintf(stderr,".");putc('\n',stderr);};history= 2;jump_out();};
  179. input_organization[next_available_file++].name_of_file=  *argv;
  180. }
  181. }
  182.  
  183.  
  184. /*:7*/
  185. #line 83 "tie.web"
  186. ;
  187. /*30:*/
  188. #line 374 "tie.web"
  189.  
  190. common_init();
  191. for(i= 0;i<next_available_file;i++){
  192. input_organization[i].mode= 1;
  193. input_organization[i].lineno= 0;
  194. input_organization[i].type_of_file= 2;
  195. input_organization[i].limit= 0;
  196. for(j= 0;j<=100;j++)input_organization[i].buffer[j]= 0;
  197. }
  198. actual_input= 0;
  199. out_mode= 1;
  200. open_input();
  201.  
  202.  
  203. /*:30*/
  204. #line 84 "tie.web"
  205. ;
  206. /*3:*/
  207. #line 93 "tie.web"
  208.  
  209. input_has_ended= 0;
  210. while(!input_has_ended||(actual_input!=0))process_line();
  211. if(out_mode==3)
  212. write_string(stdout,"@z\n");
  213. /*41:*/
  214. #line 530 "tie.web"
  215. if(verbose)fprintf(stderr,"\n");
  216.  
  217.  
  218. #line 1 "io.web"
  219. /*:41*/
  220. #line 98 "tie.web"
  221. ;
  222.  
  223.  
  224. /*:3*/
  225. #line 85 "tie.web"
  226. ;
  227. /*4:*/
  228. #line 105 "tie.web"
  229.  
  230. for(i= 1;i<next_available_file;i++)
  231. if(input_organization[i].mode!=4)
  232. {{putc('\n',stderr);fprintf(stderr,"! Change file entry did not match");};err_loc(i);history= 1;};
  233.  
  234.  
  235. /*:4*/
  236. #line 86 "tie.web"
  237. ;
  238.  
  239. /*51:*/
  240. #line 20 "history.web"
  241.  
  242. switch(history){
  243. case 0:if(verbose){fprintf(stderr,"(No errors were found.)");putc('\n',stderr);};break;
  244. case 1:{fprintf(stderr,"(Pardon me, but I think I spotted something wrong.)");putc('\n',stderr);};break;
  245. case 2:{fprintf(stderr,"(That was a fatal error, my friend.)");putc('\n',stderr);};break;
  246. }
  247. #line 1 "error.web"
  248. /*:51*/
  249. #line 88 "tie.web"
  250.  
  251. jump_out();
  252. }
  253.  
  254. /*:2*//*13:*/
  255. #line 177 "tie.web"
  256.  
  257. boolean lines_dont_match(i,j)
  258. int i,j;
  259. {
  260. buffer_index k,lmt;
  261. if(input_organization[i].limit!=input_organization[j].limit)
  262. return 1;
  263. lmt= input_organization[i].limit;
  264. for(k= 0;k<lmt;k++)
  265. if(input_organization[i].buffer[k]!=input_organization[j].buffer[k])
  266. return 1;
  267. return 0;
  268. }
  269. /*:13*//*15:*/
  270. #line 194 "tie.web"
  271.  
  272. boolean e_of_ch_module(i)
  273. int i;
  274. {
  275. return((input_organization[i].limit>=2)&&
  276. (input_organization[i].buffer[0]=='@')&&
  277. ((input_organization[i].buffer[1]=='Z')||
  278. (input_organization[i].buffer[1]=='z')));
  279. }
  280.  
  281. /*:15*//*17:*/
  282. #line 209 "tie.web"
  283.  
  284. boolean e_of_ch_preamble(i)
  285. int i;
  286. {
  287. return((input_organization[i].limit>=2)&&
  288. (input_organization[i].buffer[0]=='@')&&
  289. ((input_organization[i].buffer[1]=='Y')||
  290. (input_organization[i].buffer[1]=='y')));
  291. }
  292.  
  293. /*:17*//*20:*/
  294. #line 228 "tie.web"
  295.  
  296. boolean file_open(f,name)
  297. text_file *f;
  298. name_type name;
  299. {
  300. return(( *f= fopen(name,"r"))!=NULL);
  301. }
  302.  
  303. /*:20*//*22:*/
  304. #line 240 "tie.web"
  305.  
  306. /*25:*/
  307. #line 297 "tie.web"
  308.  
  309. init_change_file(i,b)
  310. int i;
  311. boolean b;
  312. {
  313. /*26:*/
  314. #line 312 "tie.web"
  315.  
  316. while(1){
  317. get_ln_from_file(i,input_files[i]);;
  318. if(input_organization[i].mode==4)return;
  319. if(input_organization[i].limit<2)continue;
  320. if(input_organization[i].buffer[0]!='@')continue;
  321. if((input_organization[i].buffer[1]>='X')
  322. &&(input_organization[i].buffer[1]<='Z'))
  323. input_organization[i].buffer[1]+= 'z'-'Z';
  324. if(input_organization[i].buffer[1]=='x')break;
  325. if((input_organization[i].buffer[1]=='y')||
  326. (input_organization[i].buffer[1]=='z'))
  327. if(b)
  328. {{putc('\n',stderr);fprintf(stderr,"! Where is the matching @x?");};err_loc(i);history= 1;};
  329.  
  330. }
  331.  
  332.  
  333.  
  334. /*:26*/
  335. #line 302 "tie.web"
  336. ;
  337. /*27:*/
  338. #line 333 "tie.web"
  339.  
  340. do{
  341. get_ln_from_file(i,input_files[i]);;
  342. if(input_organization[i].mode==4){
  343. {{putc('\n',stderr);fprintf(stderr,"! Change file ended after @x");};err_loc(i);history= 1;};
  344.  
  345. return;
  346. }
  347. }while(input_organization[i].limit<=0);
  348.  
  349.  
  350. /*:27*/
  351. #line 303 "tie.web"
  352. ;
  353. }
  354.  
  355.  
  356. /*:25*/
  357. #line 241 "tie.web"
  358.  
  359. open_input()
  360. {
  361. int i;
  362. /*23:*/
  363. #line 271 "tie.web"
  364.  
  365. if(!file_open(&(input_files[0]),input_organization[0].name_of_file)){
  366. {{putc('\n',stderr);fprintf(stderr,"! Master file can\'t be opened");};{fprintf(stderr,".");putc('\n',stderr);};history= 2;jump_out();};
  367. }
  368.  
  369. else{
  370. if(verbose){
  371. fprintf(stderr,"(");
  372. print_name_of_file(0);
  373. {fprintf(stderr,")");putc('\n',stderr);};
  374. }
  375. input_organization[0].type_of_file= 1;
  376. get_ln_from_file(0,input_files[0]);;
  377. }
  378.  
  379.  
  380. /*:23*/
  381. #line 245 "tie.web"
  382. ;
  383. actual_input= 0;
  384. no_ch= 0;
  385. while(++no_ch<next_available_file){
  386. if(!file_open(&(input_files[no_ch]),input_organization[no_ch].name_of_file)){
  387. {{putc('\n',stderr);fprintf(stderr,"! Change file can\'t be opened");};{fprintf(stderr,".");putc('\n',stderr);};history= 2;jump_out();};
  388. }
  389.  
  390. else{
  391. if(verbose){
  392. fprintf(stderr,"(");
  393. print_name_of_file(no_ch);
  394. {fprintf(stderr,")");putc('\n',stderr);};
  395. }
  396. init_change_file(no_ch,1);
  397. }
  398. }
  399.  
  400. if(--no_ch==0)
  401. {{putc('\n',stderr);fprintf(stderr,"! No change file found");};{fprintf(stderr,".");putc('\n',stderr);};history= 2;jump_out();};
  402.  
  403. for(i= next_available_file;i<=9;i++)
  404. input_organization[i].mode= 4;
  405. }
  406.  
  407. /*:22*//*28:*/
  408. #line 347 "tie.web"
  409.  
  410. put_line(j)
  411. int j;
  412. {
  413. int i;
  414. for(i= 0;i<input_organization[j].limit;i++)
  415. fputc(xchr[input_organization[j].buffer[i]],stdout);
  416. fputc('\n',stdout);
  417. }
  418.  
  419. /*:28*//*29:*/
  420. #line 358 "tie.web"
  421.  
  422. write_string(fp,s)
  423. text_file fp;
  424. char *s;
  425. {
  426. while( *s!='\0'){
  427. putc( *s,fp);s++;}
  428. }
  429.  
  430.  
  431.  
  432.  
  433. /*:29*//*32:*/
  434. #line 394 "tie.web"
  435.  
  436. process_line()
  437. {
  438. int i;
  439. /*39:*/
  440. #line 521 "tie.web"
  441.  
  442. if(++lines_processed%100==0&&verbose){
  443. if(lines_processed%500==0)
  444. fprintf(stderr,"%d",lines_processed);
  445. else
  446. fprintf(stderr,".");
  447. }
  448.  
  449. /*:39*/
  450. #line 398 "tie.web"
  451. ;
  452. /*33:*/
  453. #line 410 "tie.web"
  454.  
  455. while(e_of_ch_module(actual_input)){
  456. if(input_organization[actual_input].type_of_file==1)
  457.  
  458. {{putc('\n',stderr);fprintf(stderr,"! This can\'t happen");};{fprintf(stderr,".");putc('\n',stderr);};history= 2;jump_out();};
  459. input_organization[actual_input].mode= 1;
  460. init_change_file(actual_input,1);
  461. while((input_organization[actual_input].mode!=3)
  462. &&(actual_input>0))actual_input--;
  463. }
  464. if(input_has_ended&&(actual_input==0))return;
  465. test_input= (next_available_file);
  466. i= actual_input;
  467. while((test_input==(next_available_file))&&(i<no_ch)){
  468. i++;
  469. switch(input_organization[i].mode){
  470. case 1:if(!lines_dont_match(actual_input,i)){
  471. input_organization[i].mode= 2;
  472. test_input= i;
  473. }
  474. break;
  475. case 2:
  476. if(lines_dont_match(actual_input,i))
  477. {
  478.  
  479. input_organization[i].mode= 1;
  480. {{putc('\n',stderr);fprintf(stderr,"! Sections do not match");};err_loc(actual_input);history= 1;};
  481.  
  482. err_loc(i);
  483. init_change_file(i,0);
  484. }
  485. else test_input= i;
  486. break;
  487. case 3:;
  488. break;
  489. case 4:;
  490. break;
  491. }
  492. }
  493.  
  494.  
  495. /*:33*/
  496. #line 399 "tie.web"
  497. ;
  498. /*34:*/
  499. #line 451 "tie.web"
  500.  
  501. if(prod_chf){
  502. while(1){
  503. /*35:*/
  504. #line 464 "tie.web"
  505.  
  506. {
  507. if(out_mode==1)
  508. if(test_input!=(next_available_file)){
  509. write_string(stdout,"@x\n");
  510. out_mode= 2;
  511. }
  512. else break;
  513. }
  514.  
  515.  
  516. /*:35*/
  517. #line 454 "tie.web"
  518. ;
  519. /*36:*/
  520. #line 477 "tie.web"
  521.  
  522. {
  523. if(out_mode==2){
  524. if(test_input==(next_available_file)){
  525. write_string(stdout,"@y\n");
  526. out_mode= 3;
  527. }else{
  528. if(input_organization[actual_input].type_of_file==1)
  529. put_line(actual_input);
  530. break;
  531. }
  532. }
  533. }
  534.  
  535.  
  536. /*:36*/
  537. #line 455 "tie.web"
  538. ;
  539. /*37:*/
  540. #line 494 "tie.web"
  541.  
  542. {
  543. if(out_mode==3){
  544. if(input_organization[actual_input].type_of_file==2){
  545. if(test_input==(next_available_file))put_line(actual_input);
  546. break;
  547. }else{
  548. write_string(stdout,"@z\n");
  549. out_mode= 1;
  550. }
  551. }
  552. }
  553.  
  554.  
  555. /*:37*/
  556. #line 456 "tie.web"
  557. ;
  558. }
  559. }else
  560. if(test_input==(next_available_file))put_line(actual_input);
  561.  
  562.  
  563. /*:34*/
  564. #line 400 "tie.web"
  565. ;
  566. /*38:*/
  567. #line 508 "tie.web"
  568.  
  569. get_ln_from_file(actual_input,input_files[actual_input]);;
  570. if(test_input!=(next_available_file)){
  571. get_ln_from_file(test_input,input_files[test_input]);;
  572. if(e_of_ch_preamble(test_input)){
  573. get_ln_from_file(test_input,input_files[test_input]);;
  574. input_organization[test_input].mode= 3;
  575. actual_input= test_input;
  576. test_input= (next_available_file);
  577. }
  578. }
  579.  
  580. /*:38*/
  581. #line 402 "tie.web"
  582. ;
  583. }
  584.  
  585.  
  586. /*:32*//*45:*/
  587. #line 54 "io.web"
  588.  
  589. get_ln_from_file(i,cur_file)
  590. int i;
  591. text_file cur_file;
  592. {
  593. int final_limit;
  594. int c;
  595. if(input_organization[i].mode==4)return;
  596. if(feof(cur_file))/*46:*/
  597. #line 67 "io.web"
  598.  
  599. {
  600. input_organization[i].mode= 4;
  601. if(input_organization[i].type_of_file==1)input_has_ended= 1;
  602. return;
  603. }
  604.  
  605.  
  606. /*:46*/
  607. #line 62 "io.web"
  608. ;
  609. /*47:*/
  610. #line 79 "io.web"
  611.  
  612. (input_organization[i].lineno)++;
  613. input_organization[i].limit= 0;final_limit= 0;
  614. while((input_organization[i].limit<=100)&&(c= getc(cur_file))!=EOF
  615. &&c!='\n')
  616. {
  617. input_organization[i].buffer[input_organization[i].limit]= xord[c];
  618. (input_organization[i].limit)++;
  619. if((input_organization[i].buffer[input_organization[i].limit-1]!=' ')
  620. &&(input_organization[i].buffer[input_organization[i].limit-1]
  621. !='\t'))
  622. final_limit= input_organization[i].limit;
  623. }
  624. if(input_organization[i].limit>100)
  625. if((c= getc(cur_file))!=EOF&&c!='\n'){
  626. ungetc(c,cur_file);{{putc('\n',stderr);fprintf(stderr,"! Input line too long");};err_loc(i);history= 1;};
  627.  
  628. }
  629. /*48:*/
  630. #line 101 "io.web"
  631.  
  632. if(input_organization[i].limit==0&&c==EOF)
  633. /*46:*/
  634. #line 67 "io.web"
  635.  
  636. {
  637. input_organization[i].mode= 4;
  638. if(input_organization[i].type_of_file==1)input_has_ended= 1;
  639. return;
  640. }
  641.  
  642.  
  643. /*:46*/
  644. #line 103 "io.web"
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651. #line 1 "history.web"
  652. /*:48*/
  653. #line 97 "io.web"
  654.  
  655. input_organization[i].limit= final_limit;
  656.  
  657. /*:47*/
  658. #line 63 "io.web"
  659. ;
  660. }
  661.  
  662. /*:45*//*58:*/
  663. #line 96 "character.web"
  664.  
  665. common_init()
  666. {
  667. strcpy(xchr,"                                 !\"#$%&'()*+,-./0123456789\
  668. :;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ");
  669. /*60:*/
  670. #line 127 "character.web"
  671.  
  672.  
  673. /*:60*//*64:*/
  674. #line 551 "tie.web"
  675.  
  676. #line 552 "tie.web"
  677. xchr['\t']= '\t';
  678.  
  679.  
  680. /*:64*/
  681. #line 101 "character.web"
  682. ;
  683. /*59:*/
  684. #line 108 "character.web"
  685. {
  686. int i;
  687. for(i= 0;i<=0177;i++)xord[i]= '\040';
  688. for(i= 1;i<0177;i++)xord[xchr[i]]= i;
  689. }
  690.  
  691. /*:59*/
  692. #line 102 "character.web"
  693. ;
  694. }
  695.  
  696. /*:58*/
  697.